import ApiTubro from '@/utils/Engineering/ApiTubro';
import { apiPath } from './private.config.data';
import { openModalVisible, generateUUID } from '@/utils/utils';
import { tableInsideOperateRender } from './operate-view';

export default function operateMode(this: any) {
  //复制
  this['handleSingleCopy'] = async function(record: any, index: number, extraData: any = {}) {
    const { searchDataList } = this.state;
    let { id, editable, originalRecord, key, ...__sent_data } = record;
    if (extraData.__sent_data_diff_cover_field) {
      __sent_data[extraData.__sent_data_diff_cover_field] = generateUUID().substr(-12);
    }
    let isok = await ApiTubro.onSingleInsert(apiPath.submitDataBaseURL, {
      __sent_data,
      okText: '复制成功',
      errorText: '复制失败',
    });
    isok && this.getDatasource({ __sent_data: searchDataList, pursueId: record.id });
  };

  this['handleQuickEdit'] = async function(e: any, is_editable: boolean) {
    this.state.dataSource.map((item: any) => {
      item.editable = is_editable;
    });
    this.setState(({ dataSource, unquickable }: { dataSource: any; unquickable: any }) => ({
      dataSource,
      unquickable: !unquickable,
    }));
  };

  this['handleSingleQuickEdit'] = async function({
    editable,
    record,
    index,
    submit,
  }: {
    unquickable: boolean;
    index: number;
    record: any;
    e?: any;
    originalRecord?: any;
    submit?: boolean;
    editable?: any;
  }): Promise<any> {
    record.editable = editable;
    const is_all_editable = this.state.dataSource.find(
      (item: { editable: boolean }) => item.editable == true,
    );
    this.state.unquickable = is_all_editable ? false : true;
    if (submit) {
      //点击表格操作确认按钮的时候
      let { editable, originalRecord, key, ...__sent_data } = record;
      let isok = await ApiTubro.onSingleUpdate(apiPath.submitDataBaseURL, {
        __sent_data,
      });
      if (isok) {
        if (this.state.unquickable == true) {
          const { searchDataList } = this.state;
          this.getDatasource({ __sent_data: searchDataList });
        } else {
          let { originalRecord, ...__originalRecord } = record;
          this.state.dataSource[index].originalRecord = __originalRecord;
          this.setState(({ dataSource }: { dataSource: any }) => ({ dataSource }));
        }
      } else {
        this.state.dataSource[index] = {
          ...record.originalRecord,
          originalRecord: record.originalRecord,
          editable: true,
        };
        this.setState(({ dataSource }: { dataSource: any }) => ({ dataSource }));
      }
    } else {
      //点击表格操作取消按钮的时候
      this.state.dataSource[index] = {
        ...record.originalRecord,
        originalRecord: record.originalRecord,
        editable: false,
      };
      this.setState(({ dataSource }: { dataSource: any }) => ({ dataSource }));
    }
  };

  this['handleSingleDelete'] = async function(e: any, record: any) {
    const { searchDataList } = this.state;
    let isok = await ApiTubro.onSingleDelete(apiPath.submitDataBaseURL, {
      id: record[apiPath.basis || 'id'],
    });
    isok && this.getDatasource({ __sent_data: searchDataList });
  };

  this['handleModalEdit'] = async function(e: any, record: any, index: number) {
    const { dataSource } = this.state;
    openModalVisible({
      selfTarget: this,
      visibleName: 'modal_visible',
      isVisible: true,
      title: '弹窗编辑',
    });
    this.state.allFieldTarget = dataSource[index];
    this.setState({
      allFieldTarget: dataSource[index],
      __sent_data_field_id: record.id,
    });
  };

  // 弹窗提交
  this['handleModalSubmit'] = async function(e: any) {
    const { searchDataList } = this.state;
    const { __sent_data_field_id } = this.state;
    let isok = this.PopupPartTarget.onSubmit(__sent_data_field_id);
    isok && this.getDatasource({ __sent_data: searchDataList }); //todo 有争议
    openModalVisible({ selfTarget: this, visibleName: 'modal_visible', isVisible: false });
  };

  this['handleOperaateJump'] = async function(e: any, record: any, _letterPoint: any, item: any) {
    let getCode = record.code || record.sys_form_set_code || record.sys_table_set_code;
    _letterPoint.setState({
      activeKey: item.key,
      allFieldToValueObj: {
        sys_table_set_code: getCode,
        sys_form_set_code: getCode,
        code: getCode,
      },
    });
  };
  this['tableInsideOperateRender'] = function() {
    return {
      title: '操作',
      dataIndex: '__operate',
      key: '__operate',
      width: 240,
      fixed: 'right',
      render: (text: any, record: any, index: number) =>
        tableInsideOperateRender({
          text,
          record,
          index,
          selfTarget: this,
          _letterPoint: this.props._letterPoint,
        }),
    };
  };
}
